package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Announcement;
import com.bbm.communitysystem.domain.bo.AnnouncementBo;
import com.bbm.communitysystem.domain.dto.AnnouncementDto;
import com.bbm.communitysystem.domain.vo.AnnouncementPageVo;
import com.bbm.communitysystem.domain.vo.HomeAnnouncementPageVo;
import com.bbm.communitysystem.domain.vo.PageVo;
import com.bbm.communitysystem.service.IAnnouncementService;
import com.bbm.communitysystem.tools.DemoDate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Tag(name = "公告")
@RestController
@RequestMapping("/announcement")
@RequiredArgsConstructor
public class AnnouncementController {

    private final IAnnouncementService announcementService;

    @Operation(summary = "获取最新公告", description = "首页Vo")
    @GetMapping("/new")
    public R<HomeAnnouncementPageVo> getNewAnnouncement() {

        AnnouncementDto newAnnouncement = announcementService.getNewAnnouncement();

        HomeAnnouncementPageVo homeAnnouncementPageVo = new HomeAnnouncementPageVo();

        homeAnnouncementPageVo.setId(newAnnouncement.getId());
        homeAnnouncementPageVo.setTitle(newAnnouncement.getAnnTitle());
        return R.ok(homeAnnouncementPageVo);
    }

    @Operation(summary = "新增公告")
    @PostMapping
    public R addAnnouncement(@RequestBody AnnouncementDto announcementDto) {

        AnnouncementBo announcementBo = new AnnouncementBo();
        announcementBo.setAnnContect(announcementDto.getAnnContect());
        announcementBo.setAnnContent(announcementDto.getAnnContent());
        announcementBo.setAnnTitle(announcementDto.getAnnTitle());
        announcementBo.setUpdateBy(announcementDto.getUpdateBy());

        AnnouncementDto announcementDto1 = announcementService.addAnnouncement(announcementBo);
        if (announcementDto1 == null) {
            return R.error("新增失败");
        }

        return R.ok("新增成功", announcementDto1);
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "公告id")
    public R delAnnouncement(@PathVariable("id") Integer id) {

        boolean b = announcementService.removeById(id);

        if (!b) {
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }

    @Operation(summary = "修改公告")
    @PutMapping
    public R putAnnouncement(@RequestBody AnnouncementDto announcementDto) {

        Announcement announcement = BeanUtil.copyProperties(announcementDto, Announcement.class);



        boolean update = announcementService.lambdaUpdate()
                .eq(Announcement::getId, announcement.getId())

                .set(announcementDto.getAnnTitle() != null &&!announcementDto.getAnnTitle().isEmpty() ,Announcement::getAnnTitle, announcement.getAnnTitle())
                .set(announcementDto.getAnnContent() != null &&!announcementDto.getAnnContent().isEmpty() ,Announcement::getAnnContent, announcement.getAnnContent())
                .set(announcementDto.getAnnContect() != null &&!announcementDto.getAnnContect().isEmpty() ,Announcement::getAnnContect, announcement.getAnnContect())

                .set(Announcement::getAnnTime, System.currentTimeMillis())
                .set(Announcement::getStatus,announcementDto.getStatus())
                .set(Announcement::getUpdateBy, announcement.getUpdateBy())
                .set(Announcement::getUpdateTime, System.currentTimeMillis())
                .update();

        if (!update) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    @Operation(summary = "发布公告")
    @PutMapping("/public")
    public R publicAnnouncement(@RequestBody AnnouncementDto announcementDto) {

        boolean update = announcementService.lambdaUpdate()
                .set(Announcement::getStatus, 2)
                .set(Announcement::getAnnTime, System.currentTimeMillis())
                .set(Announcement::getUpdateTime, System.currentTimeMillis())
                .set(Announcement::getUpdateBy, announcementDto.getUpdateBy())
                .eq(Announcement::getId, announcementDto.getId())
                .update();

        return update ? R.ok("获取成功") : R.error("获取失败");
    }

    @Operation(summary = "获取所有公告", description = "暂时无Vo")
    @GetMapping("/all")
    @Parameter(name = "page", description = "页数")
    @Parameter(name = "size", description = "每页数量")
    public R getAllAnnouncement(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Announcement> announcementPage = new Page<>(page, size);

        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();

        Page<Announcement> page1 = announcementService.page(announcementPage, queryWrapper);

        PageVo<AnnouncementPageVo> announcementPageVoPage = new PageVo<>(page1);

        page1.getRecords().forEach(item -> {
            AnnouncementPageVo announcementPageVo = getAnnouncementPageVo(item);

            String lookUser = item.getLookUser();

            if (lookUser != null) {
                announcementPageVo.setLookUser(lookUser.split(","));
            } else {
                announcementPageVo.setLookUser(new String[]{});
            }

            announcementPageVoPage.getRecords().add(announcementPageVo);
        });

        return R.ok("获取成功", announcementPageVoPage);
    }

    @Operation(summary = "获取公告", description = "暂时无Vo")
    @GetMapping("/{id}")
    public R<AnnouncementPageVo> getAnnouncement(@PathVariable("id") Integer id) {

        Announcement announcement = announcementService.getById(id);

        if (announcement == null) {
            return R.error("获取失败");
        }

        AnnouncementPageVo announcementPageVo = getAnnouncementPageVo(announcement);

        return R.ok("获取成功", announcementPageVo);
    }

    @Operation(summary = "获取公告", description = "暂时无Vo")
    @GetMapping("/ann")
    public R<AnnouncementPageVo> getAnnouncement(@RequestParam("userId") Integer userId,
                                                 @RequestParam("annId") Integer annId) {

        Announcement announcement = announcementService.getById(annId);

        announcement.setLookUser(announcement.getLookUser() + "," + userId);

        boolean b = announcementService.updateById(announcement);

        AnnouncementPageVo announcementPageVo = getAnnouncementPageVo(announcement);

        announcementPageVo.setPublicTime(DemoDate.getPostDate(announcement.getAnnTime()));
        announcementPageVo.setLookUser(announcement.getLookUser().split(","));


        return R.ok(announcementPageVo);
    }




    private AnnouncementPageVo getAnnouncementPageVo(Announcement item) {

        if (item == null) {
            return null;
        }

        AnnouncementPageVo announcementPageVo = new AnnouncementPageVo();

        announcementPageVo.setTitle(item.getAnnTitle());

        announcementPageVo.setPublicTime(item.getAnnTime());

        if (item.getStatus() == 2) {
            announcementPageVo.setPublicTime(DemoDate.getPostDate(item.getAnnTime()));
        }


        announcementPageVo.setId(item.getId());
        announcementPageVo.setContent(item.getAnnContent());
        announcementPageVo.setStatus(item.getStatus());
        announcementPageVo.setContect(item.getAnnContect());

        return announcementPageVo;
    }

}
